package com.etc.zzsys.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.zzsys.entity.Question;
import com.etc.zzsys.entity.User;
import com.etc.zzsys.service.UserService;
import com.etc.zzsys.service.impl.UserServiceImpl;
import com.etc.zzsys.util.MD5Util;
import com.etc.zzsys.util.PageData;
import com.sun.beans.editors.IntegerEditor;

/**
 * Servlet implementation class Myservlet
 */
@WebServlet("/Userservlet")
public class Userservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Userservlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		request.setCharacterEncoding("utf-8");

		UserService us = new UserServiceImpl();
		String op = request.getParameter("op");

		if ("list".equals(op)) {
			doQuery(request, response);
		}

		if ("add".equals(op)) {
			doAdd(request, response);
		}

		if ("del".equals(op)) {
			doDel(request, response);
		}

		if ("update".equals(op)) {
			doUpdate(request, response);
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		doGet(request, response);
	}

	protected void doQuery(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		UserService us = new UserServiceImpl();
		int page=1;
		int pageSize=3;
		String key="";
		if(request.getParameter("page")!=null) {
			page=Integer.valueOf(request.getParameter("page"));
		}
		if(request.getParameter("pageSize")!=null) {
			pageSize=Integer.valueOf(request.getParameter("pageSize"));
		}
		if(request.getParameter("key")!=null) {
			key=request.getParameter("key");
		}
		if(request.getParameter("key")!=null) {
			key=request.getParameter("key");
		}
		request.setAttribute("key",key);
		PageData<User> pdata=us.queryQuestionByPage(page, pageSize, key);
		List<User> ulist=us.getUsers();
		request.setAttribute("ulist", ulist);
		request.setAttribute("pdata", pdata);
		request.getRequestDispatcher("user.jsp").forward(request, response);
	}

	protected void doUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		UserService us = new UserServiceImpl();
		int userId = Integer.valueOf(request.getParameter("userId"));
		String userName = request.getParameter("userName");
		String userSex = request.getParameter("userSex");
		String userTel = request.getParameter("userTel");
		String userAddress = request.getParameter("userAddress");
		int userLevel = Integer.valueOf(request.getParameter("userLevel"));
		int userState = Integer.valueOf(request.getParameter("userState"));

		User u = new User(userId, userName, userSex, userTel, userAddress, userLevel, userState);

		us.updateUser(u);
	}
	protected void doAdd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		UserService us = new UserServiceImpl();
		User u = new User();
		u.setUserName(request.getParameter("userName"));
		u.setUserPwd(MD5Util.getEncodeByMd5(request.getParameter("userPwd")));
		u.setUserSex(request.getParameter("userSex"));
		u.setUserTel(request.getParameter("userTel"));
		u.setUserAddress(request.getParameter("userAddress"));
		u.setUserLevel(Integer.valueOf(request.getParameter("userLevel")));
		u.setUserState(Integer.valueOf(request.getParameter("userState")));
		boolean result = us.addUser(u);
		if (result) {
			request.getRequestDispatcher("Userservlet?op=list").forward(request, response);
		}
	}
	protected void doDel(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		UserService us = new UserServiceImpl();
		int userId = Integer.valueOf(request.getParameter("id"));
		boolean result = us.delUser(userId);
		if (result) {
			request.getRequestDispatcher("Userservlet?op=list").forward(request, response);
		}
	}
}
